#include "Items.h"
#include <fstream>
#include <assert.h>
#include <string>
#include <stdio.h>
//#include <sstream>
#include "World/Entity/Statsheet.h"
#include "Global References/Globals.h"
#include "World/Entity/Local References.h"
using namespace wordModify;
using namespace std;

void Item_Equippable::_equipInit
	(EquipType a_Type, short m_P, short m_I, short m_E, short a_Extra)//Extra is either Weapon Range or Armor Penalty
{	
	if(itemName == "INVALID_REFERENCE")
		assert("Invalid _equipInit. Did you call itemInit?");
	m_placeBlock = false;
	//===BaseObject
	objectType	= TYPE_ENTITY_PIECE;		
	active		= true;
	//===Equipable		
	e_equipType = a_Type;
	m_pierce	= m_P;
	m_impact	= m_I;
	m_energy	= m_E;
	m_extraData	= a_Extra;
};
void Item_Equippable::_fileInit(char* FileName, recipe* a_Recipe)
{
	//===BaseObject
	objectType		= TYPE_ENTITY_PIECE;		
	active			= true;
	m_placeBlock	= false;

	ifstream File(FileName);
	if(File.is_open())
	{


		string trashData;
		string goodData;
		for(int i = 0; i < 4; i++)
		{
			getline(File,trashData,':');
			getline(File,goodData,';');
			a_Recipe->namIngredients[i] = wordModify::copy(a_Recipe->namIngredients[i],goodData);
		}
		//===ItemName
		getline(File,trashData,':');
		getline(File,goodData ,';');
		itemName = wordModify::copy(itemName,goodData);//(char*)(goodData.c_str());
		//===ItemType
		getline(File,trashData,':');
		getline(File,goodData ,';');
		itemType = atoi(goodData.c_str());
		a_Recipe->itemType = itemType;
		//===EquipType
		getline(File,trashData,':');
		getline(File,goodData ,';');
		e_equipType = atoi(goodData.c_str());
		//===Skill Alignment
		getline(File,trashData,':');
		getline(File,goodData ,';');
		//skillAlignment = atoi(goodData);
		for(int i = 0; i < STAT_NUMOFSTATS; i++)
			if(statNames[i] == goodData)
				skillAlignment = i;
		//===Skill Requisite
		getline(File,trashData,':');
		getline(File,goodData ,';');
		skillRequisite = atoi(goodData.c_str());
		//===Extra Data
		getline(File,trashData,':');
		getline(File,goodData ,';');
		m_extraData = atoi(goodData.c_str());
		//===Pierce
		getline(File,trashData,':');
		getline(File,goodData ,';');
		m_pierce = atoi(goodData.c_str());
		//===Impact
		getline(File,trashData,':');
		getline(File,goodData ,';');
		m_impact = atoi(goodData.c_str());
		//===Energy
		getline(File,trashData,':');
		getline(File,goodData ,';');
		m_energy = atoi(goodData.c_str());
	}
	File.close();
};
void Item_Equippable::_copyInit(Item_Equippable* toCopy)
{
	this->m_extraData		= toCopy->m_extraData;
	this->m_impact			= toCopy->m_impact;
	this->m_pierce			= toCopy->m_pierce;
	this->m_energy			= toCopy->m_energy;
	this->e_equipType		= toCopy->e_equipType;
	this->assetIndex		= toCopy->assetIndex;
	this->secassetIndex		= toCopy->secassetIndex;
	this->iconAsset			= toCopy->iconAsset;
	this->itemName			= toCopy->itemName;
	this->itemType			= toCopy->itemType;
	this->objectType		= toCopy->objectType;
	this->skillAlignment	= toCopy->skillAlignment;
	this->skillRequisite	= toCopy->skillRequisite;
	this->uniqueID			= toCopy->uniqueID;
	this->m_placeBlock		= toCopy->m_placeBlock;
}
void Item_Equippable::addPIE(short& a_pierce, short& a_impact, short& a_energy)
{
	a_pierce += m_pierce;
	a_impact += m_impact;
	a_energy += m_energy;
};
void Item_Equippable::remPIE(short& a_pierce, short& a_impact, short& a_energy)
{
	a_pierce -= m_pierce;
	a_impact -= m_impact;
	a_energy -= m_energy;
};

//=====ACCESSORS
Vector3D Item_Equippable::getPIE()		{return Vector3D(m_pierce,m_impact,m_energy);}
short Item_Equippable::getRange()		{return m_extraData;}
short Item_Equippable::getCRDPenalty()	{return m_extraData;}
short Item_Equippable::getEquipType()	{return e_equipType;}


void Item_Equippable::serialize(ByteBuffer*& info)
{
	Item_Base::serialize(info);
	info->addLong((long)secassetIndex.x);
	info->addLong((long)secassetIndex.y);
}
void Item_Equippable::deserialize(ByteBuffer* info, int & index)
{
	Item_Base::deserialize(info, index);
	secassetIndex.x = (float)info->getLong(index); index += sizeof(long);
	secassetIndex.y = (float)info->getLong(index); index += sizeof(long);
}